home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
1035
/
1035.xpi
/
chrome
/
1clickweather.jar
/
content
/
1clickweather
/
js
/
network.js
< prev
next >
Wrap
Text File
|
2008-10-05
|
3KB
|
116 lines
/*
The XMLRequest object can handle blocking and nonblocking calls.
If the setParser method is called, it will turn the object into a nonblocking object.
The setParser call requires the object that is passed into the method to have 2 methods:
parseFunc();
parseError();
Basic usage:
xmlRequest = new XMLRequest(url);
xmlRequest.setParser(parserObject);
xmlRequest.Get();
*/
function XMLRequest(url){
this.url = url;
this.xmlDoc = null;
var parseObj;
var request = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Components.interfaces.nsIXMLHttpRequest);
request.timeout = Components.classes["@mozilla.org/timer;1"].createInstance(Components.interfaces.nsITimer);
DumperTagProperties["OPTION"] = ['text','value','defaultSelected'];
this.setParser = function(TparseObj){
parseObj = TparseObj;
}
this.Get = function(){
if(this.url){
if(!parseObj){
this.xmlDoc = this.Blocking();
}else{
this.NonBlocking();
}
}else{
return(false);
}
}
this.Blocking = function(){
if(this.url){
try{
request.overrideMimeType('text/xml');
request.open('GET', this.url, false);
request.send(null);
if(request.status == 200){
return(request.responseXML);
}
}
// we catch any errors to make sure we don't fill the javascript console with junk
catch(e){
return(null);
}
}
return(null);
}
this.NonBlocking = function(){
if(this.url && parseObj){
request.overrideMimeType('text/xml');
request.open('GET', this.url, true);
// we attach an anonymous function to the onreadystatechange method of the request.
// when state changes, this function gets called
request.onreadystatechange = this.AsyncCall;
// now that we have created the request, actually send it
request.send(null);
return(false);
}else{
return(false);
}
}
this.AsyncCall = function(){
var reqState = request.readyState;
try{
switch(reqState){
// once we get to a state '4', we have finished the request so we move on
case 4:
if(request.status == 200){
//alert(request.responseText);
parseObj.parseFunc(request.responseXML);
return(true);
}else{
parseObj.parseError(request.status);
return(false);
}
break;
// if we manage to get a '2' for state, make sure there is a status number for the request.
// if we didn't get anything for the status, we have an error
case 2:
if(!request.status){
parseObj.parseError(request.status);
return(false);
}
break;
default:
// the catch all
}
}
catch(e){
parseObj.parseError(0);
return(false);
}
}
}